package cn.bluemobi.dylan.step.presenter.my.fragment;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import cn.bluemobi.dylan.step.base.RxPresenter;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Order;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.moder.http.CommonSubscriber;
import cn.bluemobi.dylan.step.moder.http.RetrofitHelper;
import cn.bluemobi.dylan.step.moder.http.RxUtil;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import cn.bluemobi.dylan.step.moder.http.response.PageDataBean;
import cn.bluemobi.dylan.step.moder.prefs.Prefs;
import cn.bluemobi.dylan.step.moder.prefs.SharedPreferencesHelper;
import cn.bluemobi.dylan.step.presenter.contract.my.MyContract;
import cn.bluemobi.dylan.step.moder.bean.StepData;
import cn.bluemobi.dylan.step.step.utils.DbUtils;
import cn.bluemobi.dylan.step.utils.common.DateUtil;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by $Liudan on 2018/8/7 0007.
 */
public class MyPresenter extends RxPresenter<MyContract.View> implements MyContract.Presenter {
    @Inject
    SharedPreferencesHelper prefHelper;

    @Inject
    public MyPresenter() {

    }

    /**
     * 获取当天步数
     */
    public void dbTodayStepcount(){

        Observable<List<StepData>> observable = Observable.create(new ObservableOnSubscribe<List<StepData>>() {
            @Override
            public void subscribe(ObservableEmitter<List<StepData>> emitter) throws Exception {
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String sdfDate =  sdf.format(date); //当前日期
                long CURRENT_DATE =DateUtil.dateToStampDay(sdfDate);
                //获取当天的数据，用于展示
                List<StepData> list = DbUtils.getQueryByWhere(StepData.class, "stepTime", new Long[]{CURRENT_DATE});

                emitter.onNext(list);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<List<StepData>> observer = new Observer<List<StepData>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(List<StepData> list) {
                if (list != null && list.size() > 0) {
                    mView.showStepcount(list.get(0));
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }

    /**
     * 获取user
     */
    public void preUser() {
        Observable<User> observable = Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> emitter) throws Exception {
                User user = prefHelper.getPrefObject(Prefs.key_user, User.class);
                emitter.onNext(user);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<User> observer = new Observer<User>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(User user) {
                if (user != null) {
                    mView.showUserInfo(user);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }


    /**
     * 获取积分
     */
    public void getMemberAccount(int memberId){
        addSubscribe(RetrofitHelper.getLoginApi().getMemberAccount(memberId)
                .compose(RxUtil.<HttpResponseBean<MemberAccount>>rxSchedulerHelper())
                .compose(RxUtil.<MemberAccount>handleResult())
                .subscribeWith(new CommonSubscriber<MemberAccount>(mView) {
                    @Override
                    public void onNext(MemberAccount user) {
                        if(mView==null)return;
                        mView.showAccountScore(user);
                    }
                })
        );
    }

    /**
     * 商品订单
     */
    public void httpGoodsOrderList(int memberId) {

        addSubscribe(RetrofitHelper.getGoodsApi().getGoodsOrderList(1, 1, memberId)
                .compose(RxUtil.<HttpResponseBean<PageDataBean<List<Order>>>>rxSchedulerHelper())
                .compose(RxUtil.<PageDataBean<List<Order>>>handleResult())
                .subscribeWith(new CommonSubscriber<PageDataBean<List<Order>>>(mView) {
                    @Override
                    public void onNext(PageDataBean<List<Order>> result) {
                        if(mView!=null){
                            mView.httpOrderSuceess(result.getTotal());
                        }
                    }
                    @Override
                    protected void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        if(mView!=null){
                            mView.closeDialog();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if(mView!=null){
                            mView.closeDialog();
                        }
                    }
                })
        );
    }

}
